package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Dom4JTest1 {
    public static void main(String[] args) throws Exception {
        SAXReader saxReader = new SAXReader();

        Document document =
                saxReader.read("Day19-properties-xml-log\\src\\helloworld.xml");

        Element rootElement = document.getRootElement();
        System.out.println(rootElement.getName());

        List<Element> elements = rootElement.elements("user");
        for (Element element : elements) {
            System.out.println(element.getName());
        }

        Element peopele = rootElement.element("people");
        System.out.println(peopele.getText());

        Element user = rootElement.element("user");
        System.out.println(user.elementText("name"));

        System.out.println(user.attributeValue("name"));
        Attribute id = user.attribute("name");
        System.out.println(id.getName());
        System.out.println(id.getValue());

        List<Attribute> attributes = user.attributes();
        for (Attribute attribute : attributes) {
            System.out.println(attribute.getName() + "=" + attribute.getValue());
        }

        System.out.println(user.elementText("name"));
        System.out.println(user.elementText("地址"));
        System.out.println(user.elementText("password"));
        System.out.println(user.elementText("data"));
        Element element = user.element("data");
        System.out.println(element.getText());
        System.out.println(element.getTextTrim());


    }
}

